

# include "banner.h"

# include <malloc.h>
# include <string.h>

# include "string_banner.h"
# include "dictionary_banner.h"

typedef char zchar_8;
typedef int zint_32;
zint_32 CaseSensitiveValue = 0;


zint_32 ExtractWord(pDictionary pDictionaryTest)
{
	zchar_8 *TempWord;
	TempWord = (zchar_8*)malloc(CharMaxsize * sizeof(zchar_8));
	if (TempWord)
	{
		zchar_8 TempChar;
		zint_32 TempCharSize  = 0;
		memset(TempWord, '\0', CharMaxsize);
		
		while ((TempChar = getchar()) != EOF)
		{
			if ((TempChar >= 'A' && TempChar <= 'Z') \
					|| (TempChar >= 'a' && TempChar <= 'z'))
			{
				if (CaseSensitiveValue)
				{
					if ((TempChar >= 'A' && TempChar <= 'Z'))
						TempChar |= 0x20;
				}
				TempWord[TempCharSize] = TempChar;
				TempCharSize++;
				
				if (TempChar == 'Q')
					return 0;
			}
			else
			{
				if (TempCharSize != 0)
				{
					char *String;
					pWord pWordTest;
					pNode pNodeTest;
					pKeyValue pKeyValueTest;
					
					String = InitString(TempWord, TempCharSize);
					pWordTest = InitWord(String, TempCharSize);
					pNodeTest = InitNode(0);
					pKeyValueTest = InitKeyValue(pWordTest, pNodeTest);

					
					AddDicWord(pDictionaryTest, pKeyValueTest);
					// ShowKeyValue(pKeyValueTest);
					memset(TempWord, '\0', TempCharSize);
					TempCharSize = 0;
				}
			}
		}
	}
	else 
		return -1;
	
	return 0;

}

State_banner AddDicWord(pDictionary pDictionaryTest, pKeyValue pWordKeyTest)
{
	if (pDictionaryTest == NULL || pWordKeyTest == NULL)
	{
		return -1;
	}
	else 
	{
		pDictionaryTest->Dictionarys[pDictionaryTest->CntSize] = pWordKeyTest; // init CntSize before use
		pDictionaryTest->Dictionarys[pDictionaryTest->CntSize]->Value = 1;
		ChangeNode(pDictionaryTest->Dictionarys[pDictionaryTest->CntSize]->pNode, pDictionaryTest->CntSize + 1);
		pDictionaryTest->CntSize++;
	}
	return 0;
}

State_banner ShowDicWord(pDictionary pDictionaryTest)
{
	printf("Dictionary Size is %d\n", pDictionaryTest->CntSize);
	if (pDictionaryTest != NULL)
	{
		for (zint_32 i = 0; i < pDictionaryTest->CntSize; i++)
		{
			ShowKeyValue(pDictionaryTest->Dictionarys[i]);
			printf("\n");
		}
	}
	return 0;
}

State_banner SequenceDicWord(pDictionary pDictionaryTest)
{
	for (int i = 0; i < pDictionaryTest->CntSize-1; i++)
		for (int j = i + 1; j < pDictionaryTest->CntSize; j++)
		{
			if (CompareWord(pDictionaryTest->Dictionarys[i]->pWord, \
							pDictionaryTest->Dictionarys[j]->pWord) > 0)
			{
				pKeyValue TempKeyValue;
				TempKeyValue = pDictionaryTest->Dictionarys[i];
				pDictionaryTest->Dictionarys[i] = pDictionaryTest->Dictionarys[j];
				pDictionaryTest->Dictionarys[j] = TempKeyValue;
			}
		}
	return 0;	
}

State_banner DeleteDicWord(pDictionary pDictionaryTest, zint_32 Number)
{

	if (pDictionaryTest != NULL)
	{
		DeleteKeyValue(&pDictionaryTest->Dictionarys[Number]);
		
		for (int i = Number; i < pDictionaryTest->CntSize-1; i++)
		{
			pDictionaryTest->Dictionarys[i] = pDictionaryTest->Dictionarys[i + 1];
		}
		pDictionaryTest->Dictionarys[pDictionaryTest->CntSize-1] = NULL;
		pDictionaryTest->CntSize--;
	}
	return 0;
}


State_banner DeleteRepeatDicWord(pDictionary pDictionaryTest)
{
	for (int i = 0; i < pDictionaryTest->CntSize-1; i++)
	{
		for (int j = i + 1; j < pDictionaryTest->CntSize; j++)
		{
			if (CompareWord(pDictionaryTest->Dictionarys[i]->pWord, \
							pDictionaryTest->Dictionarys[j]->pWord) == 0)
			{
				AddNode(pDictionaryTest->Dictionarys[i]->pNode, pDictionaryTest->Dictionarys[j]->pNode);
				pDictionaryTest->Dictionarys[i]->pNode->Frequency++;
				DeleteDicWord(pDictionaryTest, j);
				j--;
			}
		}
	}
	return 0;
}
 
State_banner FrequencySequence(pDictionary pDictionaryTest)
{
	for (int i = 0; i < pDictionaryTest->CntSize-1; i++)
		for (int j = i + 1; j < pDictionaryTest->CntSize; j++)
		{
			if (pDictionaryTest->Dictionarys[i]->pNode->Frequency < pDictionaryTest->Dictionarys[j]->pNode->Frequency)
			{
				pKeyValue TempKeyValue;
				TempKeyValue = pDictionaryTest->Dictionarys[i];
				pDictionaryTest->Dictionarys[i] = pDictionaryTest->Dictionarys[j];
				pDictionaryTest->Dictionarys[j] = TempKeyValue;
			}
		}
	return 0;
}
